package com.csii.pos.uofplan;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csii.adhoc.controller.BaseController;
import com.csii.adhoc.exception.ActSubmitException;
import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.exception.UidGenerateException;
import com.csii.adhoc.id.UidGenerator;
import com.csii.adhoc.util.serializer.PeSerializerUtils;
import com.csii.pos.scaleplan.IfbScalePlan;
import com.csii.pos.uofplan.service.UofPlanDescService;
import com.csii.pos.uofplan.service.UofPlanService;
import com.csii.result.APIResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @description: 资金计划详细页面
 * @author: liuzhonghua
 * @create:
 */
@RestController
@RequestMapping("/uofPlanDesc")
@Slf4j
public class UofPlanDescController extends BaseController<UofPlanDesc, UofPlanDescService> {

    @Resource
    private UofPlanDescService uofPlanDescService;
    @Autowired
    private UidGenerator uidGenerator;
    @Autowired
    private UofPlanService uofPlanService;
    /**
     * @description: 详细计划新增
     * @author liuzhonghua
     * @date  20210830
     * @version 1.0
     */
    @RequestMapping("addDeptPlanDesc")
    public APIResult<?> addDeptPlan(@RequestBody UofPlanDesc uofPlanDesc){
        //生成编号
        long uid = 0;
        try {
            uid = uidGenerator.getUID();
        }catch (UidGenerateException e) {
            log.info("生成主键异常!");
        }
        uofPlanDesc.setUofItemDescSn(String.valueOf(uid));
        uofPlanDesc.setFiBcpNo("null");
        uofPlanDesc.setFiNo("null");
        String status = uofPlanService.findApproveStatus(uofPlanDesc.getUofItemSn());
        if ("1".equals(status) || "2".equals(status))   throw new ActSubmitException("当前数据已开始审批或审批已结束,无法新增!");
        uofPlanDescService.save(uofPlanDesc);
        return APIResult.successResult("添加成功！");
    }
    /**
     * @description: 详细计划删除
     * @author liuzhonghua
     * @date  20210830
     * @version 1.0
     */
    @RequestMapping("deleteDeptPlanDesc")
    public APIResult<?> deleteDeptPlan(@RequestBody UofPlanDesc uofPlanDesc){
        String status = uofPlanService.findApproveStatus(uofPlanDesc.getUofItemSn());
        boolean flag = false;
        if ("1".equals(status)) {
            flag = true;
        }
        if (flag) throw new ActSubmitException("选中的数据包含审批中,无法删除!");
        uofPlanDescService.removeById(uofPlanDesc.getUofItemDescSn());
        return APIResult.successResult("删除成功!");
    }
    /**
     * @description: 详细计划修改
     * @author liuzhonghua
     * @date  20210830
     * @version 1.0
     */
    @RequestMapping("editDeptPlanDesc")
    public APIResult<?> editDeptPlan(@RequestBody UofPlanDesc uofPlanDesc) {
        String status = uofPlanService.findApproveStatus(uofPlanDesc.getUofItemSn());
        if ("1".equals(status) || "2".equals(status))   throw new ActSubmitException("当前数据已开始审批或审批已结束,无法更新!");

        uofPlanDescService.updateById(uofPlanDesc);
        return APIResult.successResult("修改成功!");

    }
    /**
     * @description: 详细计划查询
     * @author liuzhonghua
     * @date  20210830
     * @version 1.0
     */
    @RequestMapping("queryDeptPlanDesc")
    public APIResult<?> queryDeptPlan(@RequestBody UofPlanDesc uofPlanDesc){
        QueryWrapper<UofPlanDesc> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(uofPlanDesc.getUofItemSn()) || StringUtils.isEmpty(uofPlanDesc.getUofItemSn())) {
            return APIResult.successResult(null);
        }
        queryWrapper.eq("UOF_ITEM_SN", uofPlanDesc.getUofItemSn());
        List<UofPlanDesc> list = uofPlanDescService.list(queryWrapper);
        return APIResult.successResult(list);
    }
}
